package henu.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.Exam;
import henu.bean.Permission;
import henu.bean.Role;
import henu.dao.PermissionDao;
import henu.dao.RoleDao;
import henu.dao.factory.DaoFactory;
import henu.util.CONSTANTS;

public class RoleDaoImpl implements RoleDao {
	private Role role ; 
	@Override
	public int save(Role role) {
		QueryRunner runner = DaoFactory.getRunner();
		int result = 0;
		// INSERT INTO test VALUES(2,1,1,2);
		String sql = "INSERT into role VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params = { null, role.getRolename(), role.getStatus(),
				role.getDesc(), role.getRemark() ,role.getXinxi(),role.getYonghu(),role.getHuiyi(),role.getTongzhi()
				,role.getJiaoyu(),role.getKaohe(),role.getHudong(),role.getXitong()};
		try {
			result = runner.update(sql, params);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		return result;
	}
	@Override
	public int delete(Integer roleid) {
		QueryRunner runner = DaoFactory.getRunner();
		int result = 0;
		String sql = "delete from role where roleid = ?";
		try {
			result = runner.update(sql, roleid);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		return result;
	}

	@Override
	public int update(int id, Role role) {
		QueryRunner runner = DaoFactory.getRunner();
		int result = 0 ;
		String sql ="" ;
		Object[] params ={1,2};
		try {
			result = runner.update(sql,params );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Role> findByProperty(String property, String key, String order,
			String sort, int start, int end) {
		List<Role> list = null;
		String sql = "SELECT * FROM (SELECT tt.*, ROWNUM ro FROM (select * from role where "
				+ property
				+ "='"
				+ key
				+ "'  order by "
				+ order
				+ " "
				+ sort
				+ ") tt WHERE ROWNUM <=" + end + ") WHERE ro > " + start + "";
		QueryRunner runner = DaoFactory.getRunner();
		try {
			list = runner.query(sql, new BeanListHandler<Role>(Role.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public Role findById(int roleid) {
		String sql ="SELECT * from role where roleid = ?" ;
		Role role  = null ; 
		QueryRunner runner = DaoFactory.getRunner();
		try {
			role = runner.query(sql, new BeanHandler<Role>(Role.class),roleid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return role;
	}
	
	/*获取一个用户的所有链接*/
	@Override
	public List<String> findAvailableUri(Integer roleid)
	{
		role = findById(roleid);
		List<String> availabel = new ArrayList<String>();  // 存放合法的namespace
		List<Permission> listPermission = null ; 
		PermissionDao daoPermission = DaoFactory.getPermissionDaoInstance();/*添加hudong 权限的namespace*/
		if(role.getHudong().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_HUDONG, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		if(role.getHuiyi().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_HUIYI, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		if(role.getJiaoyu().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_JIAOYU, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		if(role.getKaohe().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_KAOHE, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		if(role.getTongzhi().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_TONGZHI, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		if(role.getXinxi().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_XINXI, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		if(role.getXitong().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_XITONG, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		if(role.getYonghu().trim().equals("true"))
		{
			listPermission = daoPermission.findByProperty("pname", CONSTANTS.PER_YONGHU, "pid", "asc");
			for(Permission pm:listPermission)
			{
				availabel.add(pm.getUrl().trim());
			}
		}
		// test 
		return availabel;
	}
	/*查找所有角色*/
	@Override
	public List<Role> findAll(String order, String sort) {
		List<Role> list = null;
		String sql = "SELECT * FROM  role   order by "+ order+" " + sort ;
		QueryRunner runner = DaoFactory.getRunner();
		try {
			list = runner.query(sql, new BeanListHandler<Role>(Role.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}
